<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="70%" top="10vh" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false">
        <el-container>
            <el-mian>
                <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="top" style="height:450px;overflow:auto">
                    <!-- 工序 -->
                    <el-col :span="24">
                        <el-form-item label="工序信息" prop="process_data">
                            <sc-form-table ref="table" v-model="form.process_data" :addTemplate="process" placeholder="暂无数据" :hideAdd="mode=='add'" :hideDelete="mode=='add'">
                                <el-table-column width="65" v-if="mode=='edit'">
                                    <template #default="scope">
                                        <el-button type="primary" size="small" icon="el-icon-search" @click="openGong(scope.row,scope.$index)"></el-button>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="process_id" label="一级工序" align="center">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.process_id" @change="changeGong(scope.row)" placeholder="请选择" filterable :disabled="mode=='add'" clearable>
                                            <el-option v-for="item in processList" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="GongxuSon" label="二级工序" align="center" filterable>
                                    <template #default="scope">
                                        <el-select v-model="scope.row.two_process_id" placeholder="请选择" :disabled="mode=='add'" clearable>
                                            <el-option v-for="item in scope.row.GongxuSon" :key="item.id" :label="item.title" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="work_price" label="工序单价" align="center">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.work_price" placeholder="请输入工序单价" :readonly="mode=='add'" clearable></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="staff_id" label="选择员工" align="center">
                                    <template #default="scope">
                                        <el-select v-model="scope.row.staff_id" placeholder="请选择" clearable>
                                            <el-option v-for="item in scope.row.staff_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
                                        </el-select>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="finish_time" label="完成日期" align="center" v-if="mode=='edit'">
                                    <template #default="scope">
                                        <el-date-picker v-model="scope.row.finish_time" :value-format="'YYYY-MM-DD'" type="data" style="width:100%" placeholder="请选择完成日期" />
                                    </template>
                                </el-table-column>
                                <el-table-column prop="sort" label="排序" align="center" v-if="mode=='edit'">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.sort"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="remark" label="工序备注" align="center" v-if="mode=='edit'">
                                    <template #default="scope">
                                        <el-input v-model="scope.row.remark"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="staff_id" label="报工状态" align="center" v-if="mode=='add'">
                                    <template #default="scope">
                                        <el-tag v-if="scope.row.is_report == 0" type="danger">未报</el-tag>
							            <el-tag v-if="scope.row.is_report == 1" type="success">已报</el-tag>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="report" label="是否报工" align="center" v-if="mode=='add'">
                                    <template #default="scope">
                                        <el-switch v-model="scope.row.report" :active-value="1" :inactive-value="0"></el-switch>
                                    </template>
                                </el-table-column>
                            </sc-form-table>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-mian>
            <el-footer>
                <div style="display: flex;justify-content: end;">
                    <el-button @click="visible=false" >取 消</el-button>
			        <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
                </div>
            </el-footer>
        </el-container>
        <gong v-if="gong.save" ref="gongDialog" @success="gongSuccess" @closed="gong.save = false" draggable></gong>
	</el-dialog>
</template>

<script>
import Gong from './gong'
export default {
  components: { Gong },
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '报工提交',
                edit: '本厂工序设定',
                show: '查看'
            },
            dialog: {
				save: false
			},
            gong: {
				save: false
			},
            visible: false,
            isSaveing: false,
            grade:"潜力",
            //表单数据
            form: {
                id:"",
                process_data:[],
            },
            whether:"",
            process:{process_id:"",two_process_id:"",sort:"",price:"",remark:"",staff_id:""},
            humList: [],  //客户
            staffList:[],//员工
            processList:[], //工序
        }
    },
    mounted() {
        this.getStaff()
        this.getGongXu()
    },
    methods: {
        //显示
        open(mode='add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单提交方法
        submit(){
            let a = false
            if(this.mode=="add"){
                this.form.process_data.map(item=>{
                    if(item.is_report==1){
                        a = true
                    }
                })
            }
            if(a==true){
                this.$confirm(
                    '含有已报工工序,是否重复报工?',
                    '提示',
                    { confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',}
                ).then(() => {
                    this.$refs.dialogForm.validate(async (valid) => {
                        if (valid) {
                            this.isSaveing = true;
                            var res  = [];
                            res = await this.$API.develop.edition.report.post(this.form);
                            this.isSaveing = false;
                            if(res.code == 1){
                                this.$emit('success', this.form, this.mode)
                                this.visible = false;
                                this.$message.success("操作成功")
                            }else{
                                this.$alert(res.message, "提示", {type: 'error'})
                            }
                        }else{
                            return false;
                        }
                    })
                }).catch(() => {

                })
            }else{
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res  = [];
                        if (this.mode=='add') {
                            // 报工
                            res = await this.$API.develop.edition.report.post(this.form);
                        } else {
                            // 添加本厂工序
                            res = await this.$API.develop.edition.process.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res.code == 1){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success("操作成功")
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }else{
                        return false;
                    }
                })
            }
        },
        //表单注入数据
        async setData(data) {
			this.form.id = data.id
            var res = await this.$API.develop.edition.detail.get({id:data.id,is_local_process:1});
			this.form.process_data = res.data.process
            this.whether = res.data.whether
		},
        // 获取工序
        async getGongXu(){
            var res = await this.$API.all.process.get({pid:0});
            this.processList = res.data;
        },
        async change(e){
            var res = await this.$API.all.query.get({id:e});
            this.codeList = res.data; 
        },
        //所有员工
        async getStaff(){
            var res = await this.$API.all.staff.get();
            this.staffList = res.data;
        },
        openGong(row,index){
            console.log('row :>> ', row);
            this.gong.save = true
			this.$nextTick(() => {
				this.$refs.gongDialog.open('add').setData({index:index})
			})
        },
        gongSuccess(data, mode) {
            console.log('data :>> ', data);
            console.log('mode :>> ', mode);
            this.form.process_data = []
            data[0].detail.map(item=>{
                this.form.process_data.push({
                    process_id:item.process_id,
                    process_title:item.process_title,
                    price:item.price,
                    sort:item.sort,
                    remark:item.remark,
                    GongxuSon:item.GongxuSon,
                    staff_id:"",
                    staff_list:item.staff_list,
                    two_process_id:item.two_process_id,
                })
            })
		},
        async changeGong(e){
            var res = await this.$API.all.process.get({pid:e.process_id});
            e.GongxuSon = res.data
        },
    }
}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
